package com.idrl.service;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import com.ambow.core.dao.HibernateEntityDao;
import com.idrl.dao.po.Topic;

public class TopicService extends HibernateEntityDao<Topic>{
	private static Logger log = Logger.getLogger(TopicService.class);

	
	public List<Topic> getTopicList(Integer[] ids) 
	{
		if(log.isDebugEnabled())
		{
			log.debug("ids=" + ids);
		}		
	
		StringBuffer sb = new StringBuffer("from Topic o where o.id in (0");
		
		for(int i = 0; i < ids.length; i++)
		{
			sb.append(",?");
		}
		sb.append(") order by id desc");
		
		List<Topic>  result = this.createQuery(sb.toString(),	ids).list();
				
		if(log.isDebugEnabled())
		{
			log.debug(result == null ? "result is null!" : "result.size=" + result.size());
		}
		return result;
	}

	public List<Topic> getDayList(Long id, Date currDate) {
		if(log.isDebugEnabled())
		{
			log.debug("userid=" + id);
			log.debug("currDate=" + currDate);
		}		
	
		String sd = DateFormatUtils.format(currDate, "yyyy-MM-dd 00:00:00");
		String ed = DateFormatUtils.format(currDate, "yyyy-MM-dd 23:59:59");		
		
		List<Topic>  result = this.createQuery(
				"from Topic o where o.owner.id=? and (o.pubdate between '"
				    + sd +"' and '" + ed + "') order by id desc",
				id).list();
				
		if(log.isDebugEnabled())
		{
			log.debug(result == null ? "result is null!" : "result.size=" + result.size());
		}
		return result;
	}
	
	public List<Topic> getMonthList(Long id, Date currDate) {
		if(log.isDebugEnabled())
		{
			log.debug("userid=" + id);
			log.debug("currDate=" + currDate);
		}		
	
		String sd = DateFormatUtils.format(currDate, "yyyy-MM-01 00:00:00");
		String ed = DateFormatUtils.format(currDate, "yyyy-MM-31 23:59:59");		
		
		List<Topic>  result = this.createQuery(
				"from Topic o where o.owner.id=? and (o.pubdate between '"
				    + sd +"' and '" + ed + "') order by id desc",
				id).list();
				
		if(log.isDebugEnabled())
		{
			log.debug(result == null ? "result is null!" : "result.size=" + result.size());
		}
		return result;
	}

	
	public List<Topic> getTopicList(Long id, int default_size) 
	{
		List<Topic>  result = this.createQuery(
				"from Topic o where o.owner.id=?  order by id desc", id)
				.setFirstResult(0).setMaxResults(default_size).list();
		
		return result;
	}

	public List<Topic> getTopicList(int default_size) {
		List<Topic>  result = this.createQuery(
				"from Topic o  order by id desc")
				.setFirstResult(0).setMaxResults(default_size).list();
		
		return result;
	}
	
	
}